package org.tigris.atlas.xml.schema.mda.element;

import java.util.Collection;

import org.apache.commons.lang.StringUtils;
import org.tigris.atlas.mda.metadata.element.Enumeration;
import org.tigris.atlas.mda.metadata.element.Field;

public class SchemaField implements Field {
	
	private Field field;
	
	/**
	 * Create a new instance of <tt>Field</tt> with the correct functionality set 
	 * to generate Schema code
	 * @param fieldToDecorate The <tt>Field</tt> to decorate
	 */
	public SchemaField(Field fieldToDecorate) {
		if (fieldToDecorate == null) {
			throw new IllegalArgumentException("SchemaFields must be instatiated with a non-null field!");
		}
		field = fieldToDecorate;
	}

	public String getColumn() {
		return field.getColumn();
	}

	public String getGenerator() {
		return field.getGenerator();
	}

	public String getLabel() {
		return field.getLabel();
	}

	public String getName() {
		return field.getName();
	}
	
	public String getDocumentation() {
		return field.getDocumentation();
	}

	public String getType() {
		return field.getType();
	}

	public String getCapitalizedName() {
		return StringUtils.capitalize(getName());
	}

	public String getUppercasedName() {
		return field.getName().toUpperCase();
	}

	public String getUppercasedType() {
		return field.getType().toUpperCase();
	}

	public Boolean isSimpleType() {
		return field.isSimpleType();
	}

	public Boolean isEnumerationType() {
		return field.isEnumerationType();
	}

	public Enumeration getEnumeration() {
		Enumeration e = field.getEnumeration();
		return (e != null) ? new SchemaEnumeration(e) : null;
	}

	public String getMaxLength() {
		return field.getMaxLength();
	}

	public boolean hasMaxLength() {
		return field.hasMaxLength();
	}

	public String getMinLength() {
		return field.getMinLength();
	}

	public boolean hasMinLength() {
		return field.hasMinLength();
	}

	public String getMaxValue() {
		return field.getMaxValue();
	}

	public boolean hasMaxValue() {
		return field.hasMaxValue();
	}

	public String getMinValue() {
		return field.getMinValue();
	}

	public boolean hasMinValue() {
		return field.hasMinValue();
	}

	public String getRequired() {
		return field.getRequired();
	}

	public boolean isRequired() {
		return field.isRequired();
	}

	public String getSourceName() {
		return field.getSourceName();
	}
	
	//schema-specific generation methods:
	
	public String getSchemaType() {
		return SchemaElementUtils.getSchemaType(getType());
	}
	
	/**
	 * Return namespace name of this instance (ie, the 'xs' part of 'xmlns:xs')
	 */
	public String getNamespaceName() {
		String name;
		if (isEnumerationType().booleanValue()) {
			name = new SchemaEnumeration(getEnumeration()).getNamespaceName();
		} else {
			name = "xs";
		}
		return name;
	}
	
	/**
	 * Returns relative location of the namespace
	 */
	public String getNamespaceLocation() {
		String location = null;
		if (isEnumerationType().booleanValue()) {
			location = SchemaElementUtils.getNamespaceLocation(null, getType());
		} 
		return location;
	}

	public String getScale() {
		return field.getScale();
	}

	public boolean hasScale() {
		return field.hasScale();
	}

	public String getProject() {
		return field.getProject();
	}

	public boolean isExternal() {
		return field.isExternal();
	}
	
	public boolean hasFormat() {
		return field.hasFormat();
	}
	
	public String getFormat() {
		return field.getFormat();
	}

}
